package cn.youyeku.zsxh.ui.activity.ABS;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import cn.youyeku.zsxh.Utils.StringUtils;
import cn.youyeku.zsxh.api.ResponseStatus;

/**
 * Created by wings on 2016/9/20.
 *
 * ActionBar 使用手册
 *
 * http://blog.csdn.net/thinkingword/article/details/7830230
 */
public abstract class HandleActivity extends AppCompatActivity implements ResponseStatus{


    protected ActionBar mActionBar;
    protected String mTitle;
    protected String mSubTitle;
    protected Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case STATUS_SUCCESS:
                    onSuccess();
                    break;
                case CHANGE_MESSAGE:
                    onChange();
                default:
                    onError();
                    break;

            }
        }
    };

    protected abstract void onChange();

    protected abstract void onError();

    protected abstract void onSuccess();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }
    // 初始化ActionBar
    protected void initActionBar() {
        mActionBar = getSupportActionBar();

        // 在ActionBar左边显示'<'图标，在应用中可以通过点击此图标回到应用的主页。
        int flags = ActionBar.DISPLAY_HOME_AS_UP;
        int change = mActionBar.getDisplayOptions() ^ flags;
        mActionBar.setDisplayHomeAsUpEnabled(true);
        // 设置返回的图标
        mActionBar.setDisplayOptions(change, flags);
        if (mTitle != null && !StringUtils.isEmpty(mTitle)) {
            mActionBar.setTitle(mTitle);
        }
        if (mSubTitle != null && !StringUtils.isEmpty(mSubTitle)) {
            mActionBar.setSubtitle(mSubTitle);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void setActionBarTitle(String title) {
        mActionBar.setTitle(title);
    }

    public void setActionBarSubTitle(String subTitle) {
        mActionBar.setSubtitle(subTitle);
    }


    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {

        //设定图标 暂时没用到

        return super.onMenuOpened(featureId, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
                onBackPressed();
                break;
        }
        return true;
    }

    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    public static void FramentThreadShortToast(final Context context,
                                                  final String msg) {
        if (context != null && msg != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                }
            });
        }
    }



}
